package com.zcxy.fast.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.fast.bean.PageQuery;
import com.zcxy.fast.bean.TreeNode;
import com.zcxy.fast.entity.SysCategory;
import com.zcxy.fast.mapper.SysCategoryMapper;
import com.zcxy.fast.mybatisExtend.BaseServiceImpl;
import com.zcxy.fast.mybatisExtend.RelationHelper;
import com.zcxy.fast.service.ISysCategoryService;
import com.zcxy.fast.utils.BeanCopyUtil;
import com.zcxy.fast.utils.StringUtils;
import com.zcxy.fast.utils.TreeUtil;
import com.zcxy.fast.vo.SysCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 分类字典业务层处理
 *
 * @author zcc
 * @email  860474958@qq.com
 * @date   2020-12-25 17:39:33
 */
@Service
@Slf4j
public class SysCategoryServiceImpl extends BaseServiceImpl<SysCategoryMapper, SysCategory> implements ISysCategoryService {

    @Override
    public IPage<SysCategoryVo> queryPageList(PageQuery<SysCategory> queryVo) {
        SysCategory sysCategory = queryVo.getT() == null ? new SysCategory() : queryVo.getT();

        if (sysCategory != null && StringUtils.isEmpty(sysCategory.getParentId())) {
            sysCategory.setParentId("0");
        }
        QueryWrapper queryWrapper = new QueryWrapper(sysCategory);
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());

        IPage<SysCategory> sysUserVoIPage = baseMapper.selectPage(page, queryWrapper);
        IPage<SysCategoryVo> categoryVoIPage = BeanCopyUtil.copyPage(sysUserVoIPage, SysCategoryVo.class);
        if (CollectionUtil.isNotEmpty(categoryVoIPage.getRecords())) {
            RelationHelper.renderList(categoryVoIPage.getRecords());
        }
        return categoryVoIPage;
    }

    @Override
    public List<SysCategoryVo> queryList(SysCategory sysCategory) {
        SysCategory sysCategoryq=sysCategory == null ? new SysCategory() : sysCategory;
        QueryWrapper queryWrapper = new QueryWrapper(sysCategoryq);
        List<SysCategoryVo> SysCategoryVos=BeanCopyUtil.copyListToList(baseMapper.selectList(queryWrapper),SysCategoryVo.class);
        return SysCategoryVos;
    }

    @Override
    public List<Map> queryTreeNodeList(String parentId) {
        List<Map> treeNodes = new ArrayList<>();
        LambdaQueryWrapper<SysCategory> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isEmpty(parentId)) {
            queryWrapper.eq(SysCategory::getParentId, "0");
        } else {
            queryWrapper.eq(SysCategory::getParentId, parentId);
        }
        List<SysCategory> sysCategories = baseMapper.selectList(queryWrapper);
        List<SysCategoryVo> categoryVos = BeanCopyUtil.copyListToList(sysCategories, SysCategoryVo.class);
        RelationHelper.renderList(categoryVos);
        if (CollectionUtil.isNotEmpty(categoryVos)) {
            categoryVos.forEach(i -> {
                TreeNode treeNode = new TreeNode();
                treeNode.setKey(i.getId());
                treeNode.setLabel(i.getName());
                treeNode.setHasChildren(i.isHasChildren());
                treeNode.setParentId(i.getParentId());
                treeNodes.add(treeNode.getAttributes());
            });
        }
        return treeNodes;
    }

    @Override
    public List<TreeNode> queryTreeNodeList() {
        List<SysCategory> sysCategories = baseMapper.selectList(new QueryWrapper<>());
        List<TreeNode> treeNodes = new ArrayList<>();
        List<TreeNode> allNodes=BeanCopyUtil.copyListToList(sysCategories,TreeNode.class,(s,t)->{
            t.setKey(s.getId());
            t.setParentId(s.getParentId());
            t.setLabel(s.getName());
        });
        if(CollectionUtil.isNotEmpty(allNodes)){
            treeNodes = allNodes.stream().filter(i -> {
                return i.getParentId().equals("0");
            }).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(treeNodes)) {
                treeNodes.forEach(i -> {
                    TreeUtil.deepGenTree(allNodes,i);
                });
            }
        }
        return treeNodes;
    }


    @Override
    public List<TreeNode> queryTreeNodeListByFirstCategoryCode(String categoryCode) {
        List<SysCategory> sysCategories = baseMapper.selectList(new QueryWrapper<>());
        List<TreeNode> treeNodes = new ArrayList<>();
        if(StringUtils.isEmpty(categoryCode))
            return treeNodes;
        List<TreeNode> allNodes=BeanCopyUtil.copyListToList(sysCategories,TreeNode.class,(s,t)->{
            t.setKey(s.getId());
            t.setParentId(s.getParentId());
            t.setLabel(s.getName());
            t.setCode(s.getCode());
        });
        if(CollectionUtil.isNotEmpty(allNodes)){
            treeNodes = allNodes.stream().filter(i -> {
                return i.getParentId().equals("0")&&categoryCode.equals(i.getCode());
            }).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(treeNodes)) {
                treeNodes.forEach(i -> {
                    TreeUtil.deepGenTree(allNodes,i);
                });
            }
        }
        return treeNodes;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSysCategory(SysCategory sysCategory) {
        if (StringUtils.isEmpty(sysCategory.getParentId())) {
            sysCategory.setParentId("0");
        }
        baseMapper.insert(sysCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSysCategory(SysCategory sysCategory) {

        if (StringUtils.isEmpty(sysCategory.getParentId())) {
            sysCategory.setParentId("0");
        }
        baseMapper.updateById(sysCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAllSysCategory(List<String> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);
    }



}
